package com.yang.thread;

import java.util.Scanner;

/**
 * Java多线程学习主类
 * 提供菜单选择不同的多线程示例
 */
public class ThreadLearningMain {
    
    public static void main(String[] args) {
        System.out.println("===========================================");
        System.out.println("        Java多线程学习示例集合");
        System.out.println("===========================================");
        
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            printMenu();
            System.out.print("请选择要运行的示例 (输入数字): ");
            
            try {
                int choice = scanner.nextInt();
                
                switch (choice) {
                    case 1:
                        runBasicThreadExample();
                        break;
                    case 2:
                        runSynchronizedExample();
                        break;
                    case 3:
                        runThreadPoolExample();
                        break;
                    case 4:
                        runProducerConsumerExample();
                        break;
                    case 5:
                        runWaitNotifyExample();
                        break;
                    case 6:
                        runAdvancedThreadExample();
                        break;
                    case 7:
                        runAllExamples();
                        break;
                    case 0:
                        System.out.println("感谢使用Java多线程学习示例！再见！");
                        return;
                    default:
                        System.out.println("无效选择，请输入0-7之间的数字。");
                }
                
                System.out.println("\n按回车键继续...");
                scanner.nextLine(); // 消费换行符
                scanner.nextLine(); // 等待用户按回车
                
            } catch (Exception e) {
                System.out.println("输入错误，请输入有效的数字。");
                scanner.nextLine(); // 清除错误输入
            }
        }
    }
    
    /**
     * 打印菜单
     */
    private static void printMenu() {
        System.out.println("\n===========================================");
        System.out.println("请选择要学习的多线程示例:");
        System.out.println("1. 基础线程示例 (Thread类和Runnable接口)");
        System.out.println("2. 线程同步示例 (synchronized关键字)");
        System.out.println("3. 线程池示例 (ExecutorService)");
        System.out.println("4. 生产者消费者示例 (BlockingQueue)");
        System.out.println("5. 线程通信示例 (wait/notify机制)");
        System.out.println("6. 高级多线程示例 (Future、原子类、显式锁)");
        System.out.println("7. 运行所有示例");
        System.out.println("0. 退出程序");
        System.out.println("===========================================");
    }
    
    /**
     * 运行基础线程示例
     */
    private static void runBasicThreadExample() {
        System.out.println("\n>>> 运行基础线程示例 <<<");
        System.out.println("这个示例将演示:");
        System.out.println("- 继承Thread类创建线程");
        System.out.println("- 实现Runnable接口创建线程");
        System.out.println("- 使用Lambda表达式创建线程");
        System.out.println("- 线程的启动、等待和生命周期");
        System.out.println();
        
        BasicThreadExample.main(new String[]{});
    }
    
    /**
     * 运行线程同步示例
     */
    private static void runSynchronizedExample() {
        System.out.println("\n>>> 运行线程同步示例 <<<");
        System.out.println("这个示例将演示:");
        System.out.println("- synchronized关键字的使用");
        System.out.println("- 线程安全问题和解决方案");
        System.out.println("- 银行账户取款的并发控制");
        System.out.println("- 计数器的线程安全实现");
        System.out.println();
        
        SynchronizedExample.main(new String[]{});
    }
    
    /**
     * 运行线程池示例
     */
    private static void runThreadPoolExample() {
        System.out.println("\n>>> 运行线程池示例 <<<");
        System.out.println("这个示例将演示:");
        System.out.println("- 固定大小线程池 (FixedThreadPool)");
        System.out.println("- 缓存线程池 (CachedThreadPool)");
        System.out.println("- 单线程池 (SingleThreadExecutor)");
        System.out.println("- 定时任务线程池 (ScheduledThreadPool)");
        System.out.println("- 自定义线程池配置");
        System.out.println();
        
        ThreadPoolExample.main(new String[]{});
    }
    
    /**
     * 运行生产者消费者示例
     */
    private static void runProducerConsumerExample() {
        System.out.println("\n>>> 运行生产者消费者示例 <<<");
        System.out.println("这个示例将演示:");
        System.out.println("- 使用BlockingQueue实现生产者消费者模式");
        System.out.println("- 有界队列和无界队列的区别");
        System.out.println("- 生产速度和消费速度不匹配的处理");
        System.out.println("- 线程间的协作和通信");
        System.out.println();
        
        ProducerConsumerExample.main(new String[]{});
    }
    
    /**
     * 运行wait/notify示例
     */
    private static void runWaitNotifyExample() {
        System.out.println("\n>>> 运行wait/notify示例 <<<");
        System.out.println("这个示例将演示:");
        System.out.println("- wait()和notify()方法的使用");
        System.out.println("- 线程间的等待和通知机制");
        System.out.println("- 使用wait/notify实现生产者消费者模式");
        System.out.println("- 条件等待和唤醒");
        System.out.println();
        
        WaitNotifyExample.main(new String[]{});
    }
    
    /**
     * 运行高级多线程示例
     */
    private static void runAdvancedThreadExample() {
        System.out.println("\n>>> 运行高级多线程示例 <<<");
        System.out.println("这个示例将演示:");
        System.out.println("- Future和Callable的异步计算");
        System.out.println("- CompletableFuture的链式处理");
        System.out.println("- 原子类的无锁并发");
        System.out.println("- ReentrantLock显式锁");
        System.out.println("- ReadWriteLock读写锁");
        System.out.println();
        
        AdvancedThreadExample.main(new String[]{});
    }
    
    /**
     * 运行所有示例
     */
    private static void runAllExamples() {
        System.out.println("\n>>> 运行所有多线程示例 <<<");
        System.out.println("将依次运行所有示例，每个示例之间会有分隔线...");
        System.out.println();
        
        String[] examples = {
            "基础线程示例",
            "线程同步示例", 
            "线程池示例",
            "生产者消费者示例",
            "wait/notify示例",
            "高级多线程示例"
        };
        
        for (int i = 0; i < examples.length; i++) {
            System.out.println("\n" + "=".repeat(60));
            System.out.println("正在运行: " + examples[i] + " (" + (i+1) + "/" + examples.length + ")");
            System.out.println("=".repeat(60));
            
            switch (i) {
                case 0: BasicThreadExample.main(new String[]{}); break;
                case 1: SynchronizedExample.main(new String[]{}); break;
                case 2: ThreadPoolExample.main(new String[]{}); break;
                case 3: ProducerConsumerExample.main(new String[]{}); break;
                case 4: WaitNotifyExample.main(new String[]{}); break;
                case 5: AdvancedThreadExample.main(new String[]{}); break;
            }
            
            if (i < examples.length - 1) {
                System.out.println("\n等待3秒后继续下一个示例...");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
        
        System.out.println("\n" + "=".repeat(60));
        System.out.println("所有示例运行完毕！");
        System.out.println("=".repeat(60));
    }
}
